﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.ViewModels;
using Fayshine.VA.Helpers;

namespace Fayshine.VA.Views.SysConfig
{
    public partial class DoorSettings : Page
    {
        private ShopSettingViewModel m_ViewModel = null;
        private bool m_IsConfigUser = true;
        public DoorSettings()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DoorSettings_Loaded);
        }

        void DoorSettings_Loaded(object sender, RoutedEventArgs e)
        {
            ValidUserRole();
            m_ViewModel = new ShopSettingViewModel();
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllGatesIncludeArea();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnAddDoor_Click(object sender, RoutedEventArgs e)
        {
            if (cmbShops.SelectedIndex < 0)
            {
                MessageBox.Show("请选择店铺！");
                return;
            }

            if (string.IsNullOrEmpty(txtDoor.Text))
            {
                MessageBox.Show("门面名称不能为空！");
                return;
            }
            if (!m_ViewModel.ExistDoor(txtDoor.Text))
            {
                gate door = new RIAService.Web.gate();
                door.Name = txtDoor.Text;
                door.ParentID = (int)cmbShops.SelectedValue;
                m_ViewModel.AddGate(door);
            }
            else
            {
                MessageBox.Show("门面 " + txtDoor.Text + " 已经存在！");
            }

        }

        private void btnUpdateDoor_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtDoor.Text))
            {
                MessageBox.Show("门面名称不能为空！");
                return;
            }
            gate selectDoor = listSelectedDoors.SelectedItem as gate;
            if (selectDoor != null)
            {
                selectDoor.Name = txtDoor.Text;
                if (m_ViewModel != null)
                {
                    m_ViewModel.Update(selectDoor);
                }
            }
        }

        private void btnDeleteDoor_Click(object sender, RoutedEventArgs e)
        {
            gate selectDoor = listSelectedDoors.SelectedItem as gate;
            if (selectDoor != null)
            {
                if (m_ViewModel != null)
                {
                    m_ViewModel.DeleteGate(selectDoor);
                }
            }
            else
            {
                MessageBox.Show("请选择门面！");
            }
        }

        private void cmbShops_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbShops.SelectedIndex < 0)
            {
               // this.btn1.IsEnabled = false;
               // this.btn2.IsEnabled = false;
                btnAddDoor.IsEnabled = false;
                btnUpdateDoor.IsEnabled = false;
                btnDeleteDoor.IsEnabled = false;
            }
            else
            {
               // this.btn1.IsEnabled = true;
               // this.btn2.IsEnabled = true;
                btnAddDoor.IsEnabled = true;
                btnUpdateDoor.IsEnabled = true;
                btnDeleteDoor.IsEnabled = true;
                txtDoor_TextChanged(null, null);
                listSelectedDoors_SelectionChanged(null, null);
                if (m_ViewModel != null)
                {
                    m_ViewModel.UpdateSelectArea((int)cmbShops.SelectedValue);
                }
            }


        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            gate selectDoor = listSelectedDoors.SelectedItem as gate;
            if (selectDoor != null)
            {
                selectDoor.ParentID = null;
                if (m_ViewModel != null)
                {
                    m_ViewModel.RemoveDoor(selectDoor);
                }
            }
        }

        private void listSelectedDoors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (m_IsConfigUser)
            {
                gate selectDoor = listSelectedDoors.SelectedItem as gate;
                if (selectDoor != null)
                {
                    txtDoor.Text = selectDoor.Name;
                    this.btnDeleteDoor.IsEnabled = true;
                    this.btnUpdateDoor.IsEnabled = true;
                }
                else
                {
                    this.btnDeleteDoor.IsEnabled = false;
                    this.btnUpdateDoor.IsEnabled = false;
                }
            }
        }

        private void txtDoor_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtDoor.Text) || cmbShops.SelectedIndex<0)
            {
                this.btnAddDoor.IsEnabled = false;
                this.btnUpdateDoor.IsEnabled = false;
            }
            else
            {
                this.btnAddDoor.IsEnabled = true;

                gate selectGate = this.listSelectedDoors.SelectedItem as gate;
                if (selectGate != null)
                {
                    this.btnUpdateDoor.IsEnabled = true;
                }
                else
                {
                    this.btnUpdateDoor.IsEnabled = false;
                }
            }
        }

        private void ValidUserRole()
        {
            user user = SessionManager.Session["UserInfo"] as user;
            if (user != null)
            {
                if (user.RoleID == 3)
                {
                    m_IsConfigUser = false;
                    this.btnAddDoor.IsEnabled = false;
                    this.btnDeleteDoor.IsEnabled = false;
                    this.btnUpdateDoor.IsEnabled = false;
                    this.txtDoor.IsEnabled = false;
                }
                else
                {
                    m_IsConfigUser = true;
                    this.txtDoor.IsEnabled = true;
                    this.btnAddDoor.IsEnabled = true;
                    this.btnDeleteDoor.IsEnabled = true;
                    this.btnUpdateDoor.IsEnabled = true;
                }
            }
        }

    }
}
